package com.zonhar.autofeed.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonhar.autofeed.dao.SensorLogDao;
import com.zonhar.autofeed.entities.entity.SensorLogEntity;
import com.zonhar.autofeed.service.SensorLogService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SensorLogServiceImpl extends ServiceImpl<SensorLogDao, SensorLogEntity> implements SensorLogService {
    @Override
    public void saveLog(JSONObject json,String sensorName) {
        SensorLogEntity sensorLogEntity =new SensorLogEntity();
        sensorLogEntity.setSensorName(sensorName);
        json.put("year", DateUtil.format(new Date(),"yyyy"));
        json.put("month", DateUtil.format(new Date(),"MM"));
        json.put("day", DateUtil.format(new Date(),"dd"));
        json.put("time", DateUtil.format(new Date(),"hh:mm"));
        sensorLogEntity.setData(JSON.toJSONString(json));
        sensorLogEntity.setCreateDate(new Date());
        this.save(sensorLogEntity);
    }

    @Override
    public List<SensorLogEntity> pageList(Integer size) {
        Page<SensorLogEntity> page =new Page<>();
        page.setCurrent(1L);
        page.setSize(size);
        QueryWrapper<SensorLogEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<SensorLogEntity> sensorLogEntityPage = page(page, queryWrapper);
        return sensorLogEntityPage.getRecords();
    }
}
